package com.xhh.nphm.web.controller.system;

import com.alibaba.fastjson.JSON;
import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.system.domain.*;
import com.xhh.nphm.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 首页实时满意情况报表控制层
 */
@RestController
@RequestMapping("/system/report/mail")
public class ReportMailController extends BaseController {
    private final String prefix = "system/report/mail";

    @Autowired
    private IMailGetDealService mailGetDealService;
    @Autowired
    private MmailWorkFormService mmailWorkFormService;
    @Autowired
    private MwfSendBackService mwfSendBackService;

    /**
     * 进入办理量列表页面
     */
    @GetMapping("/reportOver")
    public ModelAndView reportOver() {
        return new ModelAndView(prefix + "/reportOver");
    }

    /**
     * 加载办理量列表数据
     * @param sendBackEty 封装查询条件
     */
    @PostMapping("/reportOverListJson")
    public TableDataInfo reportOverListJson(MwfSendBack sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sbdeptId",getSysUser().getDeptId().toString());
        params.put("startTime",getStartDate());
        params.put("endTime",getEndDate());
        List<Map<String, Object>> list = mwfSendBackService.rptReplyOverListJson(params);
        return getDataTable(list);
    }
    /**
     * 进入未转派列表页面
     */
    @GetMapping("/sendWait")
    public ModelAndView sendWait() {
        return new ModelAndView(prefix + "/sendWait");
    }
    /**
     * 加载未转派列表数据
     * @param sendBackEty 封装查询条件
     */
    @PostMapping("/sendWaitListJson")
    public TableDataInfo sendWaitListJson(MwfSendBack sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sbdeptId",getSysUser().getDeptId().toString());
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list;
        if (IS_GAOXIN()){
            list = mwfSendBackService.rptSendWaitListJson_gxq(params);
        }else{
            list = mwfSendBackService.rptSendWaitListJson(params);
        }
        return getDataTable(list);
    }

    /**
     * 进入被市级拒退工单页面
     */
    @GetMapping("/notPass")
    public ModelAndView notPass() {
        return new ModelAndView(prefix + "/notPass");
    }

    /**
     * 加载被市级拒退工单列表数据
     * @param sendBackEty 封装查询条件
     */
    @PostMapping("/notPassListJson")
    public TableDataInfo notPassListJson(MwfSendBack sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sbdeptId",getSysUser().getDeptId().toString());
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list = mwfSendBackService.rptNotPassListJson(params);
        return getDataTable(list);
    }
    /**
     * 进入临期工单列表页面
     *
     * @return 视图
     */
    @GetMapping("/muBlueS")
    public ModelAndView muBlueS() {
        return new ModelAndView(prefix + "/muBlueS");
    }


    /**
     * 加载临期工单列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/muBlueJson")
    public TableDataInfo muBlueJson(MwfSendBack sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sbdeptId",getSysUser().getDeptId().toString());
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list = mwfSendBackService.muBlueJson(params);
        return getDataTable(list);
    }
    /**
     * 进入超期工单列表页面
     *
     * @return 视图
     */
    @GetMapping("/muRedS")
    public ModelAndView muRedS() {
        return new ModelAndView(prefix + "/muRedS");
    }


    /**
     * 加载超期工单列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/muRedSJson")
    public TableDataInfo muRedSJson(MwfSendBack sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sbdeptId",getSysUser().getDeptId().toString());
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list = mwfSendBackService.muRedJson(params);
        return getDataTable(list);
    }

    /**
     * 进入承办单位未签收M列表页面
     * @return 视图
     */
    @GetMapping("/listSonSignNot")
    public ModelAndView listSonSignNot() {
        return new ModelAndView(prefix + "/listSonSignNot");
    }

    /**
     * 加载承办单位未签收M列表数据
     * @return 列表数据
     */
    @PostMapping("/SonSignNotJson")
    public TableDataInfo listSonSignNotJson(MwfSendBack sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sdeptId",getSysUser().getDeptId().toString());
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list;
        if(IS_GAOXIN()){
            list = mwfSendBackService.SonSignNotJson_gxq(params);
        }else{
            list = mwfSendBackService.SonSignNotJson(params);
        }
        return getDataTable(list);
    }

    /**
     * 进入承办单位已退工单T列表页面
     */
    @GetMapping("/listSonAgreeBack")
    public ModelAndView listSonAgreeBack() {
        return new ModelAndView(prefix + "/listSonAgreeBack");
    }

    /**
     * 加载承办单位已退T列表数据
     */
    @PostMapping("/listSonAgreeBackJson")
    public TableDataInfo listSonAgreeBackJson(MwfSendBack sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sdeptId",getSysUser().getDeptId().toString());
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list;
        if(IS_GAOXIN()){
            list = mwfSendBackService.SonAgreeBackJson_gxq(params);
        }else{
            list = mwfSendBackService.SonAgreeBackJson(params);
        }
        return getDataTable(list);
    }
    /**
     * 查询信件工单 查看页面
     *
     * @param pkId
     * @param map
     * @return
     */
    @GetMapping("/mailOrderDetail/{pkId}")
    public ModelAndView mailOrderDetail(@PathVariable Integer pkId, ModelMap map) {
        MmailWorkForm tworkFormBaseEty = mmailWorkFormService.getById(pkId);
        MmailGetDealEty ttelGetDealEty = mailGetDealService.getById(tworkFormBaseEty.getOriginFK());
        if (tworkFormBaseEty != null) {
            map.put("workFormBase", tworkFormBaseEty);
            map.put("telGetDeal", ttelGetDealEty);
        }
        return new ModelAndView(prefix + "/mailOrderDetail");
    }
    /**
     * 进入未签收工单列表页面
     *
     * @return 视图
     */
    @GetMapping("/muSignNot")
    public ModelAndView muSignNot() {
        return new ModelAndView(prefix + "/muSignNot");
    }


    /**
     * 加载未签收工单列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/muSignNotJson")
    public TableDataInfo muSignNotJson(MwfSendBack sendBackEty) {
        startPage();
        List<Map<String, Object>> list = mwfSendBackService.rptSignNotJson(getSysUser().getDeptId().toString(),sendBackEty);
        return getDataTable(list);
    }

    /**
     * 进入已退工单列表页面
     *
     * @return 视图
     */
    @GetMapping("/muDeptBack")
    public ModelAndView muDeptBack() {
        return new ModelAndView(prefix + "/muDeptBack");
    }


    /**
     * 加载已退工单列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/muDeptBackJson")
    public TableDataInfo muDeptBackJson(MwfSendBack sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sbdeptId",getSysUser().getDeptId().toString());
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list = mwfSendBackService.rptDeptBackJson(params);
        return getDataTable(list);
    }

    /**
     * 查询下级部门的未签收、未回复、退回和超期情况数据统计
     * 2022/12/29
     * @Param deptId 部门编号
     */
    @PostMapping("/listSubStatisticsJson")
    public List<Map<String, Object>> listSubStatisticsJson(ModelMap modelMap) {
        Long deptId = getSysUser().getDeptId();
        List<Map<String, Object>> mwfSendBackStaVOList = mwfSendBackService.listSubStatisticsJson(deptId,IS_GAOXIN());
        modelMap.put("mwfSendBackStaVOList",mwfSendBackStaVOList);
        //model.addAttribute("s",s);
        return mwfSendBackStaVOList;
    }

    /**
     * 下级部门电话工单详情列表
     * @param subDeptId 下级部门
     * @return 视图
     */
    @GetMapping("/subDeptMailDetail/{subDeptId}")
    public ModelAndView subDeptMailDetail(@PathVariable("subDeptId") String subDeptId) {
        return new ModelAndView(prefix + "/subDeptMailDetail");
    }

    /**
     * 下级部门信件工单详情列表
     * @param subDeptId 下级部门
     * @return 视图
     */
    @PostMapping("/subDeptMailDetailJson/{subDeptId}")
    public TableDataInfo subDeptMailDetailJson(@PathVariable("subDeptId") String subDeptId, MwfSendBack mwfSendBack) {
        startPage();
        List<Map<String,Object>> list;
        if (IS_GAOXIN()){
            list =  mwfSendBackService.subDeptMailDetailJson_gxq(subDeptId,mwfSendBack);
        }else {
            list =  mwfSendBackService.subDeptMailDetailJson(subDeptId,mwfSendBack);
        }
        return getDataTable(list);
    }

    /**
     * 获取起始时间
     * @return 起始时间
     */
    private Date getStartDate(){
        Calendar startDate = Calendar.getInstance();
        if (!isOverTwentyFive()) {
            startDate.set(Calendar.MONTH,startDate.get(Calendar.MONTH) - 1);
        }
        startDate.set(Calendar.DAY_OF_MONTH,25);
        startDate.set(Calendar.HOUR_OF_DAY,0);
        startDate.set(Calendar.MINUTE,0);
        startDate.set(Calendar.SECOND,0);
        return startDate.getTime();
    }

    /**
     * 获取结束时间
     * @return 结束时间
     */
    private Date getEndDate(){
        Calendar endDate = Calendar.getInstance();
        if (isOverTwentyFive()) {
            endDate.set(Calendar.MONTH,endDate.get(Calendar.MONTH) + 1);
        }
        endDate.set(Calendar.DAY_OF_MONTH,26);
        endDate.set(Calendar.HOUR_OF_DAY,0);
        endDate.set(Calendar.MINUTE,0);
        endDate.set(Calendar.SECOND,0);
        return endDate.getTime();
    }

    /**
     * 当前日期是否超出25号
     * @return 布尔
     */
    private boolean isOverTwentyFive(){
        Calendar curDate = Calendar.getInstance();
        boolean isOverTwentyFive = false;
        int day = curDate.get(Calendar.DAY_OF_MONTH);
        if (day >= 25) {
            isOverTwentyFive = true;
        }
        return isOverTwentyFive;
    }
}
